#include "light_func.h"

EventBits_t uxBits_TL;
EventBits_t uxBits_TR;
EventBits_t uxBits_PL;
EventBits_t uxBits_PR;
EventBits_t uxBits_Swipe;
EventBits_t uxBits_LShow;
EventBits_t uxBits_LightNormal;

extern EventGroupHandle_t xEventGroup;
extern TaskHandle_t pwm_led_handle;
extern TaskHandle_t can_task_handle;
extern TaskHandle_t tps_task_handle;
extern TaskHandle_t ic_task_handle;
extern TaskHandle_t get_adc_handle;
extern TaskHandle_t lin_handle;
extern TimerHandle_t xTimer_40ms;
extern TimerHandle_t xTimer_50ms;
extern TimerHandle_t xTimer_30ms;

uint16_t g_16SetPwmDuty[16][12] = 
{
	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
};

uint16_t g_16FenderSetPwmDuty[3][12] = 
{
	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

};

uint8_t g_u8turn_left_cnt;
uint8_t g_u8turn_right_cnt;
uint8_t g_u8Fender_turn_cnt;

void DI_FlowOn(uint8_t direction, uint8_t led_position);
void DI_On(uint8_t direction);
void DI_Off(uint8_t direction);
void PO_Left_On(void);
void PO_Left_Off(void);
void PO_Right_On(void);
void PO_Right_Off(void);
void set_pwm_blocking(uint8_t dev, uint16_t pwm[]);
void TPS_PWM_UPDATE(uint16_t pwm_arr[][12]);

/*------------------------------------------------------*/
void Light_Normal_Func(void)
{
    /*等待接收事件组标志位*/
    uxBits_TL = xEventGroupWaitBits(xEventGroup, EVENRGROUP_TL, pdFALSE, pdTRUE, 0);
    uxBits_TR = xEventGroupWaitBits(xEventGroup, EVENRGROUP_TR, pdFALSE, pdTRUE, 0);
    uxBits_PL = xEventGroupWaitBits(xEventGroup, EVENRGROUP_PL, pdFALSE, pdTRUE, 0);
    uxBits_PR = xEventGroupWaitBits(xEventGroup, EVENRGROUP_PR, pdFALSE, pdTRUE, 0);
    uxBits_Swipe = xEventGroupWaitBits(xEventGroup, EVENRGROUP_SWIPE, pdFALSE, pdTRUE, 0);

    if((uxBits_TL & EVENRGROUP_TL) == EVENRGROUP_TL) 
    {
        //TL ON
        if((uxBits_Swipe & EVENRGROUP_SWIPE) == EVENRGROUP_SWIPE)
        {
            DI_FlowOn(DEV_LEFT, g_u8turn_left_cnt);
            if(g_u8turn_left_cnt < TRUNK_TURN_LEFT_CNT)
			{
				g_u8turn_left_cnt++;
			}
        }
        else
        {
            DI_On(DEV_LEFT);
        }
    }
    else
    {
        DI_Off(DEV_LEFT);
    }

    if((uxBits_TR & EVENRGROUP_TR) == EVENRGROUP_TR) 
    {
        //TL ON
        if((uxBits_Swipe & EVENRGROUP_SWIPE) == EVENRGROUP_SWIPE)
        {
            DI_FlowOn(DEV_RIGHT, g_u8turn_right_cnt);
            if(g_u8turn_right_cnt < TRUNK_TURN_RIGHT_CNT)
			{
				g_u8turn_right_cnt++;
			}
        }
        else
        {
            DI_On(DEV_RIGHT);
        }
    }
    else
    {
        DI_Off(DEV_RIGHT);
    }

    if((uxBits_PL & EVENRGROUP_PL) == EVENRGROUP_PL) 
    {
        PO_Left_On();
    }
    else
    {
        PO_Left_Off();
    }

    if((uxBits_PR & EVENRGROUP_PR) == EVENRGROUP_PR) 
    {
        PO_Right_On();
    }
    else
    {
        PO_Right_Off();
    }

    TPS_PWM_UPDATE(g_16SetPwmDuty);
}

/*------------------------------------------------------*/
void DI_On(uint8_t direction)
{
	uint8_t pwmduty_8byte[8];
	uint8_t pwmduty_4byte[4];

	for(uint8_t i=0; i<8; i++)
	{
		pwmduty_8byte[i] = 0xFF;
	}
	for(uint8_t i=0; i<4; i++)
	{
		pwmduty_4byte[i] = 0xFF;
	}

	if(direction == 1)
	{
		Tps_WriteRegsBlocking(TPS_CHIP_ADD_14, REG_ADD_PWM0, pwmduty_8byte, eBurst_8_Byte, TRUE);
		Tps_WriteRegsBlocking(TPS_CHIP_ADD_14, REG_ADD_PWMx(8), pwmduty_4byte, eBurst_4_Byte, TRUE);
	}
	
	if(direction == 2)
	{
		Tps_WriteRegsBlocking(TPS_CHIP_ADD_15, REG_ADD_PWM0, pwmduty_8byte, eBurst_8_Byte, TRUE);
		Tps_WriteRegsBlocking(TPS_CHIP_ADD_15, REG_ADD_PWMx(8), pwmduty_4byte, eBurst_4_Byte, TRUE);
	}
}

/*------------------------------------------------------*/
void DI_FlowOn(uint8_t direction, uint8_t led_position)
{
	uint8_t dev_turn;
	uint8_t pwmduty_8byte[8];
	uint8_t pwmduty_4byte[4];
	uint16_t flowon_enablebit[5] = {0x300, 0x3C0, 0x3F0, 0x3FC, 0xFFF};


	if(direction == 1)
	{
		dev_turn = TPS_CHIP_ADD_14;
	}

	if(direction == 2)
	{
		dev_turn = TPS_CHIP_ADD_15;
	}

	switch (led_position)
	{
	case 0:
		for(uint8_t i=0; i<8; i++) 
		{
			pwmduty_8byte[i] = 0;
		}
		pwmduty_4byte[0] = 0xFF;
		pwmduty_4byte[1] = 0xFF;
		break;
	case 1:
		for(uint8_t i=0; i<6; i++) 
		{
			pwmduty_8byte[i] = 0;
		}
		pwmduty_8byte[6] = 0xFF;
		pwmduty_8byte[7] = 0xFF;
		pwmduty_4byte[0] = 0xFF;
		pwmduty_4byte[1] = 0xFF;
		break;
	case 2:
		for(uint8_t i=0; i<4; i++) 
		{
			pwmduty_8byte[i] = 0;
		}
		pwmduty_8byte[4] = 0xFF;
		pwmduty_8byte[5] = 0xFF;
		pwmduty_8byte[6] = 0xFF;
		pwmduty_8byte[7] = 0xFF;
		pwmduty_4byte[0] = 0xFF;
		pwmduty_4byte[1] = 0xFF;
		break;
	case 3:
		pwmduty_8byte[0] = 0x00;
		pwmduty_8byte[1] = 0x00;
		pwmduty_8byte[2] = 0xFF;
		pwmduty_8byte[3] = 0xFF;
		pwmduty_8byte[4] = 0xFF;
		pwmduty_8byte[5] = 0xFF;
		pwmduty_8byte[6] = 0xFF;
		pwmduty_8byte[7] = 0xFF;
		pwmduty_4byte[0] = 0xFF;
		pwmduty_4byte[1] = 0xFF;
		break;
	case 4:
		pwmduty_8byte[0] = 0xFF;
		pwmduty_8byte[1] = 0xFF;
		pwmduty_8byte[2] = 0xFF;
		pwmduty_8byte[3] = 0xFF;
		pwmduty_8byte[4] = 0xFF;
		pwmduty_8byte[5] = 0xFF;
		pwmduty_8byte[6] = 0xFF;
		pwmduty_8byte[7] = 0xFF;
		pwmduty_4byte[0] = 0xFF;
		pwmduty_4byte[1] = 0xFF;
		break;
	case 5:
		pwmduty_8byte[0] = 0xFF;
		pwmduty_8byte[1] = 0xFF;
		pwmduty_8byte[2] = 0xFF;
		pwmduty_8byte[3] = 0xFF;
		pwmduty_8byte[4] = 0xFF;
		pwmduty_8byte[5] = 0xFF;
		pwmduty_8byte[6] = 0xFF;
		pwmduty_8byte[7] = 0xFF;
		pwmduty_4byte[0] = 0xFF;
		pwmduty_4byte[1] = 0xFF;
		break;
	
	default:
		break;
	}	

	Tps_WriteRegsBlocking(dev_turn, REG_ADD_PWM0, pwmduty_8byte, eBurst_8_Byte, TRUE);
	Tps_WriteRegsBlocking(dev_turn, REG_ADD_PWMx(8), pwmduty_4byte, eBurst_4_Byte, TRUE);
}

/*------------------------------------------------------*/
void DI_Off(uint8_t direction)
{
	uint8_t dev_turn;
	uint8_t pwmduty_8byte[8];
	uint8_t pwmduty_4byte[4];

	for(uint8_t i=0; i<8; i++)
	{
		pwmduty_8byte[i] = 0x0;
	}
	for(uint8_t i=0; i<4; i++)
	{
		pwmduty_4byte[i] = 0x0;
	}

	if(direction == 1)
	{

		Tps_WriteRegsBlocking(TPS_CHIP_ADD_14, REG_ADD_PWM0, pwmduty_8byte, eBurst_8_Byte, TRUE);
		Tps_WriteRegsBlocking(TPS_CHIP_ADD_14, REG_ADD_PWMx(8), pwmduty_4byte, eBurst_4_Byte, TRUE);
	}
	
	if(direction == 2)
	{
		Tps_WriteRegsBlocking(TPS_CHIP_ADD_15, REG_ADD_PWM0, pwmduty_8byte, eBurst_8_Byte, TRUE);
		Tps_WriteRegsBlocking(TPS_CHIP_ADD_15, REG_ADD_PWMx(8), pwmduty_4byte, eBurst_4_Byte, TRUE);
	}

}

/*------------------------------------------------------*/
void PO_Left_On(void)
{
	for(uint8_t i=0; i<12; i++)
	{
		g_16SetPwmDuty[TPS_CHIP_ADD_0][i] = 0xFFF;
		g_16SetPwmDuty[TPS_CHIP_ADD_1][i] = 0xFFF;
		g_16SetPwmDuty[TPS_CHIP_ADD_2][i] = 0xFFF;
		g_16SetPwmDuty[TPS_CHIP_ADD_3][i] = 0xFFF;

		g_16SetPwmDuty[TPS_CHIP_ADD_8][i] = 0xFFF;
		g_16SetPwmDuty[TPS_CHIP_ADD_9][i] = 0xFFF;
		g_16SetPwmDuty[TPS_CHIP_ADD_10][i] = 0xFFF;
	}
}

/*------------------------------------------------------*/
void PO_Left_Off(void)
{
	for(uint8_t i=0; i<12; i++)
	{
		g_16SetPwmDuty[TPS_CHIP_ADD_0][i] = 0;
		g_16SetPwmDuty[TPS_CHIP_ADD_1][i] = 0;
		g_16SetPwmDuty[TPS_CHIP_ADD_2][i] = 0;
		g_16SetPwmDuty[TPS_CHIP_ADD_3][i] = 0;

		g_16SetPwmDuty[TPS_CHIP_ADD_8][i] = 0;
		g_16SetPwmDuty[TPS_CHIP_ADD_9][i] = 0;
		g_16SetPwmDuty[TPS_CHIP_ADD_10][i] = 0;
	}
}

/*------------------------------------------------------*/
void PO_Right_On(void)
{
	for(uint8_t i=0; i<12; i++)
	{
		g_16SetPwmDuty[TPS_CHIP_ADD_4][i] = 0xFFF;
		g_16SetPwmDuty[TPS_CHIP_ADD_5][i] = 0xFFF;
		g_16SetPwmDuty[TPS_CHIP_ADD_6][i] = 0xFFF;
		g_16SetPwmDuty[TPS_CHIP_ADD_7][i] = 0xFFF;

		g_16SetPwmDuty[TPS_CHIP_ADD_11][i] = 0xFFF;
		g_16SetPwmDuty[TPS_CHIP_ADD_12][i] = 0xFFF;
		g_16SetPwmDuty[TPS_CHIP_ADD_13][i] = 0xFFF;
	}
}

/*------------------------------------------------------*/
void PO_Right_Off(void)
{
	for(uint8_t i=0; i<12; i++)
	{
		g_16SetPwmDuty[TPS_CHIP_ADD_4][i] = 0;
		g_16SetPwmDuty[TPS_CHIP_ADD_5][i] = 0;
		g_16SetPwmDuty[TPS_CHIP_ADD_6][i] = 0;
		g_16SetPwmDuty[TPS_CHIP_ADD_7][i] = 0;

		g_16SetPwmDuty[TPS_CHIP_ADD_11][i] = 0;
		g_16SetPwmDuty[TPS_CHIP_ADD_12][i] = 0;
		g_16SetPwmDuty[TPS_CHIP_ADD_13][i] = 0;
	}
}

/*------------------------------------------------------*/
void set_pwm_blocking(uint8_t dev, uint16_t pwm[])
{
	uint8_t pwmduty_8byte[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
	uint8_t pwmduty_4byte[4] = {0x00, 0x00, 0x00, 0x00};

	for(uint8_t i=0; i<8; i++)
	{
		pwmduty_8byte[i] = ((pwm[i]>>4) & 0xFF);
	}

	for(uint8_t i=0; i<4; i++)
	{
		pwmduty_4byte[i] = ((pwm[i+8]>>4) & 0xFF);
	}
	Tps_WriteRegsBlocking(dev, REG_ADD_PWM0, pwmduty_8byte, eBurst_8_Byte, TRUE);
	Tps_WriteRegsBlocking(dev, REG_ADD_PWMx(8), pwmduty_4byte, eBurst_4_Byte, TRUE);
}

/*------------------------------------------------------*/
void TPS_PWM_UPDATE(uint16_t pwm_arr[][12])
{
	static uint8_t TPS_Step_PWM = 0;
	uint8_t u8RegDataBuf[1] = {0};

	if(TPS_Step_PWM == 0)
	{
		TPS_Step_PWM = 1;
		
		set_pwm_blocking(TPS_CHIP_ADD_5, pwm_arr[5]);
		set_pwm_blocking(TPS_CHIP_ADD_0, pwm_arr[0]);
		set_pwm_blocking(TPS_CHIP_ADD_1, pwm_arr[1]);
		set_pwm_blocking(TPS_CHIP_ADD_4, pwm_arr[4]);
	}
	else if(TPS_Step_PWM == 1)
	{
		//4pcs pl
		TPS_Step_PWM = 2;

		set_pwm_blocking(TPS_CHIP_ADD_7, pwm_arr[7]);
		set_pwm_blocking(TPS_CHIP_ADD_2, pwm_arr[2]);
		set_pwm_blocking(TPS_CHIP_ADD_3, pwm_arr[3]);
		set_pwm_blocking(TPS_CHIP_ADD_6, pwm_arr[6]);
		set_pwm_blocking(TPS_CHIP_ADD_8, pwm_arr[8]);

	}
	else if(TPS_Step_PWM == 2)
	{
		TPS_Step_PWM = 0;
		
		set_pwm_blocking(TPS_CHIP_ADD_13, pwm_arr[13]);
		set_pwm_blocking(TPS_CHIP_ADD_9, pwm_arr[9]);
		set_pwm_blocking(TPS_CHIP_ADD_12, pwm_arr[12]);
		set_pwm_blocking(TPS_CHIP_ADD_10, pwm_arr[10]);
		set_pwm_blocking(TPS_CHIP_ADD_11, pwm_arr[11]);
	}
	else
	{
		TPS_Step_PWM = 0;
	}
}

void Tps_Cycle_Init(void)
{
    static uint8_t Task_Cnt = 0;

	if(((uxBits_TL & EVENRGROUP_TL) == EVENRGROUP_TL) || ((uxBits_TR & EVENRGROUP_TR) == EVENRGROUP_TR) 
		|| ((uxBits_PL & EVENRGROUP_PL) == EVENRGROUP_PL) || ((uxBits_PR & EVENRGROUP_PR) == EVENRGROUP_PR))
	{
		if(Task_Cnt  == 0)
		{
			Task_Cnt = 1;
			Tps_BC_SetPwmReqAndRefRang(0xFF, PWM_FREQ_7800HZ, REFRANG_512);
			Tps_BC_EnableAllChannel();
			Tps_WriteOneRegBlock(TPS_CHIP_ADD_14, REG_ADD_CFG_DIAGEN0, 0xF, TRUE);	//Enable Diag Register
			Tps_WriteOneRegBlock(TPS_CHIP_ADD_14, REG_ADD_CFG_DIAGEN1, 0x3, TRUE);
		}
		if(Task_Cnt  == 1)
		{
			Task_Cnt = 2;
			Tps_WriteOneRegBlock(TPS_CHIP_ADD_15, REG_ADD_CFG_DIAGEN0, 0xF, TRUE);	
			Tps_WriteOneRegBlock(TPS_CHIP_ADD_15, REG_ADD_CFG_DIAGEN1, 0x3, TRUE);
			Tps_BC_SetLock(0xFF, 0);
			Tps_BC_SetMISC7(0xFF, 0x00);
		}
		if(Task_Cnt  == 2)
		{
			Task_Cnt = 0;
			Tps_BC_SetCLR(0xFF, 0x3);
			Tps_BC_SetMISC4(0xFF, 0);
			Tps_BC_SetMISC8(0xFF, 0xC8);
		}
	}

    uxBits_LShow = xEventGroupWaitBits(xEventGroup, EVENRGROUP_LIGHT_SHOW, pdTRUE, pdTRUE, 0);
    uxBits_LightNormal = xEventGroupWaitBits(xEventGroup, EVENRGROUP_LIGHT_NORMAL, pdTRUE, pdTRUE, 0);

    if((uxBits_LShow & EVENRGROUP_LIGHT_SHOW) == EVENRGROUP_LIGHT_SHOW)
    {
        /*挂起prvFilm_LightTask任务*/
        vTaskSuspend(pwm_led_handle);
        /*暂停CAN定时器任务*/
        xTimerStop(xTimer_40ms, 10);
    }

    if((uxBits_LightNormal & EVENRGROUP_LIGHT_NORMAL) == EVENRGROUP_LIGHT_NORMAL)
    {
        /*恢复任务*/
        vTaskResume(pwm_led_handle);
        /*重启CAN定时器任务*/
        xTimerStart(xTimer_40ms, 10);
    }
}